package com.hbnu.tangyalu.thread;

public class Box {
    private int number=0;
    public  synchronized void add(){
        if (number > 20){
            try {
                wait();
            }
            catch (InterruptedException e){
                throw new RuntimeException(e);
            }
        }
        else {
            number++;
            System.out.println(Thread.currentThread().getName()+"生产了第"+number+"号牛奶");
            notifyAll();
        }
    }
    public  synchronized void get(){
        if (number <= 0){
            try {
                wait();
            }
            catch (InterruptedException e){
                throw new RuntimeException(e);
            }
        }
        else {
            System.out.println(Thread.currentThread().getName()+"购买了第"+number+"号牛奶");
            number--;
            notifyAll();
        }
    }
}
